package com.sjgs.gis.web;

import com.sjgs.gis.domain.DataFile;
import com.sjgs.gis.repository.DataFileRepository;
import com.sjgs.gis.service.DataFileService;
import com.sjgs.gis.errors.BadRequestAlertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;


@RestController
@RequestMapping("/api/datafile")
public class DataFileController {

    @Autowired
    private DataFileService service;

    @GetMapping("")
    public Flux<DataFile> findAll() {
      return service.findAll();
    }

    @GetMapping("/fkey/{fkey}")
    public Mono<DataFile> findAllByFkey(@PathVariable(value = "fkey") String fkey) {
        return this.service.findByFkey(fkey);
    }

    @PostMapping(value="",produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<DataFile> insert(@RequestBody  DataFile datafile) {
        if (datafile.getId() != null || datafile.getFkey() == null) {
            throw new BadRequestAlertException("Insert Exception", "DataFile", "Id must be null");
        }
        return this.service.save(datafile);
    }

    @PutMapping(value="",produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<DataFile> update(@RequestBody  DataFile datafile) {
        if (datafile.getId() == null || datafile.getFkey() == null) {
            throw new BadRequestAlertException("Update Exception", "DataFile", "Id is not be null");
        }
        datafile.setUpdateTime(LocalDateTime.now());
        return this.service.save(datafile);
    }

    @DeleteMapping(value="",produces = MediaType.APPLICATION_JSON_VALUE)
    public Mono<Void> delete(@RequestBody  DataFile datafile) {
        if (datafile.getId() == null || datafile.getFkey() == null) {
            throw new BadRequestAlertException("Update Exception", "DataFile", "Id is not be null");
        }
        return this.service.delete(datafile);
    }
}